package com.liushuai.eas.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liushuai.eas.common.Result;
import com.liushuai.eas.pojo.Category;
import com.liushuai.eas.pojo.Department;
import com.liushuai.eas.service.CategoryImp;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Resource
    private CategoryImp categoryImp;

    @PostMapping
    public Result<?> add(@RequestBody Category category){

        //插入category数据库
        if (categoryImp.insert(category)!=1)
            return Result.fail(category);

        return Result.success(category);
    }

    @PutMapping
    public Result<Category> update(@RequestBody Category category){
        if (categoryImp.updateById(category)!=1)
            return Result.fail(category);
        return Result.success(category);
    }

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){
        Page<Category> categoryPage = categoryImp.selectPage(new Page<Category>(pageNum, pageSize), Wrappers.<Category>lambdaQuery().like(Category::getName, search));
        return Result.success("数据加载成功",categoryPage);
    }

    @DeleteMapping
    public Result<Category> delete(@RequestBody Category category){
        if (categoryImp.deleteById(category)!=1)
            return Result.fail(category);
        return Result.success(category);
    }

    @GetMapping("/selectAll")
    public Result<List<Category>> select(){
        List<Category> categories = categoryImp.queryCategoryList();

        return Result.success(categories);
    }

}
